/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kotionokyurievna.gmail.com.bankproject.logic;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @author Kateryna_Reznik
 */
public class Account {

    private volatile int summa;
    private ReentrantLock lock;
    
    public Account(){
        summa = 0;
    }
    
    public boolean deposit( int amount ){
        if (( (amount > 0) && (amount < Integer.MAX_VALUE ) ) & 
                ( (Integer.MAX_VALUE - summa) > amount )){
           summa += amount;
           return true;
        }
        return false;        
    }
    
    public boolean withdraw( int amount ){
       if ( ( amount <= summa) && (amount > 0)){
            summa -= amount;
            return true;
       }
       else{
            return false;
       }
    }
    
    public int getSum(){
        return summa;
    }
    
    public boolean tryLock() throws InterruptedException{
        return lock.tryLock();
    }
    
    public void unlock(){
        lock.unlock();
    }
    
    public String toString(){
        return "Account summa = " + summa;
    }
    
}
